﻿Public Class Registration

    Private frmLogin As Form

    Public Sub New(ByRef lgnForm As Form)

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        ' For termination functionality
        frmLogin = lgnForm

    End Sub

    Private Sub Registration_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'DatabaseDataSet.Employer' table. You can move, or remove it, as needed.
        Me.EmployerTableAdapter.Fill(Me.DatabaseDataSet.Employer)
        'TODO: This line of code loads data into the 'DatabaseDataSet.Employee' table. You can move, or remove it, as needed.
        Me.EmployeeTableAdapter.Fill(Me.DatabaseDataSet.Employee)
        'TODO: This line of code loads data into the 'DatabaseDataSet.User' table. You can move, or remove it, as needed.
        Me.UserTableAdapter.Fill(Me.DatabaseDataSet.User)
    End Sub

    Private Sub RegistrationControl_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RegistrationControl.Load
        Dim degreeDataTable As New DatabaseDataSet.DegreeDataTable
        Me.RegistrationControl.DegreeTableAdapter.Fill(degreeDataTable)
        Me.RegistrationControl.DegreeBindingSource.DataSource = degreeDataTable
    End Sub

    Private Sub btnConfirm_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConfirm.Click
        Me.RegistrationControl.ValidateChildren()
        If Not Me.RegistrationControl.ValidateFields() Then
            Return
        End If

        InsertUserData()

        Dim table As New DatabaseDataSet.UserDataTable
        Me.UserTableAdapter.FillByUsernameAndPassword(table, Me.RegistrationControl.txtUsername.Text, Me.RegistrationControl.txtPassword.Text)
        Dim userID As Integer = table.Rows(0).Item(0)

        CheckAndInsertEmployeeData(userID)
        CheckAndInsertEmployerData(userID)

        MessageBox.Show("Account created!", "Registration", MessageBoxButtons.OK, MessageBoxIcon.Information)
        frmLogin.Show()
        Me.Hide()
    End Sub

    '===============================================================
    ' Inserting User's Data
    '---------------------------------------------------------------
    'Purpose : Inserts user's data in the database.          
    '
    ' Author : Chatzigeorgiou Konstantinos, 10 December 2012
    '===============================================================
    Private Sub InsertUserData()
        Me.UserTableAdapter.Insert(Me.RegistrationControl.txtFirstName.Text, Me.RegistrationControl.txtLastName.Text, _
                                   Me.RegistrationControl.txtUsername.Text, Me.RegistrationControl.txtPassword.Text, _
                                   Me.RegistrationControl.txtEmail.Text)
    End Sub

    '===============================================================
    ' Checking and Inserting Employee's Data
    '---------------------------------------------------------------
    'Purpose : Checks if employee's checkbox is checked. If it is
    '          checked, it inserts data to the database, else it does
    '          nothing.
    '
    ' Author : Chatzigeorgiou Konstantinos, 10 December 2012
    '===============================================================
    Private Sub CheckAndInsertEmployeeData(ByVal userID As Integer)
        If Me.RegistrationControl.chkEmployee.Checked Then
            Dim gender As String = If(Me.RegistrationControl.rbMale.Checked, "M", "F")
            Dim master As Boolean = If(Me.RegistrationControl.chkMaster.Checked, True, False)
            Dim phd As Boolean = If(Me.RegistrationControl.chkPhd.Checked, True, False)
            Dim army As Boolean = If(Me.RegistrationControl.chkArmy.Checked, True, False)

            Me.EmployeeTableAdapter.Insert(userID, Me.RegistrationControl.txtAge.Text, gender, _
                                           Convert.ToInt16(Me.RegistrationControl.cboDegree.SelectedValue), master, phd, army, _
                                           Me.RegistrationControl.cboForeignLanguage.SelectedItem)
        End If
    End Sub

    '===============================================================
    ' Checking and Inserting Employer's Data
    '---------------------------------------------------------------
    'Purpose : Checks if employer's checkbox is checked. If it is
    '          checked, it inserts data to the database, else it does
    '          nothing.
    '
    ' Author : Chatzigeorgiou Konstantinos, 10 December 2012
    '===============================================================
    Private Sub CheckAndInsertEmployerData(ByVal userID As Integer)
        If Me.RegistrationControl.chkEmployer.Checked Then
            Me.EmployerTableAdapter.Insert(userID, Me.RegistrationControl.txtCompanyTitle.Text, Me.RegistrationControl.txtCompanyDescription.Text)
        End If
    End Sub

    '===============================================================
    ' Registration close form event
    '---------------------------------------------------------------
    ' Purpose : Overides the close event of registration form
    '           Terminates the program
    '
    ' Author : John Paraskevopoulos, 6 January 2013
    '===============================================================
    Private Sub Registration_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
        Me.frmLogin.Close()
    End Sub
End Class